package com.crm.system.service.impl.worklog;

import com.beust.jcommander.internal.Lists;
import com.crm.common.exception.BaseException;
import com.crm.common.helper.TimePeriod;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.common.util.DateUtil;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.worklog.WorkLog;
import com.crm.model.qo.worklog.WorkLogQO;
import com.crm.model.vo.worklog.WorkLogAddVO;
import com.crm.model.vo.worklog.WorkLogQueryVO;
import com.crm.service.worklog.WorkLogDbService;
import com.crm.system.service.worklog.WorkLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @Description: 工作日志
 * @Author lixq-f
 * @Date 2021/6/25
 * @Version V1.0
 **/
@Service
public class WorkLogServiceImpl implements WorkLogService {

    @Autowired
    private WorkLogDbService workLogDbService;


    @Override
    public DhPageInfo<WorkLogQueryVO> selectWorkLogListByPage(Integer pageIndex, Integer pageSize,
                                                       String sortName, String sortOrder,
                                                       WorkLogQO workLogQO) {
        PageHelper.startPage(pageIndex, pageSize);
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        boolean isSuperManage = CommonUtils.isAdmin(userAccount.getUserId());

        if (null != workLogQO.getBs()) {
            if (workLogQO.getBs() == 0) {
                //管理
                if (!isSuperManage) {
                    workLogQO.setCompanyId(userAccount.getCompanyId());
                }
            } else {
                //用户
                workLogQO.setAccountId(userAccount.getAccountId());
            }
        }else {
            throw new BaseException("标识bs不可以为空！");
        }

        if (Objects.nonNull(workLogQO.getDateType())){
            Date startTime = TimePeriod.getStartTime(workLogQO.getDateType());
            Date endTime = TimePeriod.getEndTime(workLogQO.getDateType());
            workLogQO.setDateTypeStartTm(Optional.ofNullable(startTime).orElse(null));
            workLogQO.setDateTypeEndTm(Optional.ofNullable(endTime).orElse(null));
        }

        List<WorkLogQueryVO> rLst =
                workLogDbService.selectWorkLogListByPage(workLogQO, getOrder(sortName, sortOrder));
        if (CollectionUtils.isEmpty(rLst)) {
            rLst = Lists.newArrayList();
        }
        PageInfo<WorkLogQueryVO> pageInfo = new PageInfo<>(rLst);
        return new DhPageInfo<>(pageInfo, rLst);
    }

    private String getOrder(String sortName, String sortOrder) {
        String order = " g.create_tm desc";
        if (StringUtils.isEmpty(sortName) && StringUtils.isEmpty(sortOrder)) {
            return order;
        }
        if ("createTm".equals(sortName)) {
            order = " g.create_tm " + sortOrder;
        }

        return order;
    }

    @Override
    public Boolean addWorkLog(WorkLogAddVO workLogVO) {
        WorkLog workLog = getWorkLog(workLogVO);
        return workLogDbService.addWorkLog(workLog);
    }

    @Override
    public Boolean updateWorkLog(WorkLogAddVO workLogVO) {
        WorkLog workLog = getUpdateWorkLog(workLogVO);
        return workLogDbService.updateWorkLog(workLog);
    }

    private WorkLog getUpdateWorkLog(WorkLogAddVO workLogVO) {
        WorkLog workLog = new WorkLog();
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        BeanUtils.copyProperties(workLogVO, workLog);
        workLog.setUpdateTm(DateUtil.getSysDate());
        workLog.setUpdatePeopleId(userAccount.getAccountId());
        return workLog;
    }

    @Override
    public Boolean deleteById(String logId) {
        return workLogDbService.deleteById(logId);
    }


    private WorkLog getWorkLog(WorkLogAddVO workLogVO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        WorkLog workLog = new WorkLog();
        BeanUtils.copyProperties(workLogVO, workLog);
        workLog.setLogId(CommonUtils.getGUID());
        workLog.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
        workLog.setAccountId(userAccount.getAccountId());
        workLog.setCompanyId(userAccount.getCompanyId());
        workLog.setCreateTm(DateUtil.getSysDate());
        return workLog;
    }


    @Override
    public WorkLogQueryVO getLogById(String logId) {
        WorkLogQueryVO log = workLogDbService.getLogById(logId);
        return Optional.ofNullable(log).orElse(new WorkLogQueryVO());
    }



}
